package com.sromku.simple.fb.example.fragments;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;






import uk.co.senab.actionbarpulltorefresh.library.ActionBarPullToRefresh;
import uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout;
import uk.co.senab.actionbarpulltorefresh.library.listeners.OnRefreshListener;

import com.example.hal.model.Offre;
import com.example.hal.model.OffreController;
import com.sromku.simple.fb.example.OffreAddActivity;
import com.sromku.simple.fb.example.R;


import com.sromku.simple.fb.example.utils.MyUser;
import com.sromku.simple.fb.example.views.ListOffreAdapter;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;


public class OffreFragment extends ListFragment implements OnRefreshListener{

//private List<Offre> listsOffre = new ArrayList<Offre>();
	
//private List<String> listsName = null;
//private ArrayAdapter<String> offreArray;
private ListOffreAdapter offreArray;

private	PullToRefreshLayout mPullToRefreshLayout;

	/*public ActualiteFragment() {
		// TODO Auto-generated constructor stub
		super();
	}

	   @Override 
	    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
	                Bundle savedInstanceState) { 
	    

			setContentView(R.layout.actualite_layout);
		   
	        // Inflate the layout for this fragment 
	        return inflater.inflate( 

	       getResources().getIdentifier("actualite_layout", "layout", container.getContext().getPackageName()), container, false); 
	    }     
	    */
	
	/*@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		TextView textView = new TextView(getActivity());
		textView.setGravity(Gravity.CENTER);
		Bundle args = getArguments();
		textView.setText(args.getString("KEY_STRING"));
		return textView;
	}
	    
	    public static ActualiteFragment newInstance(String chaine) {
	    	ActualiteFragment fragment = new ActualiteFragment();
	    	Bundle args = new Bundle();
	    	args.putString("KEY_STRING", chaine);
	    	fragment.setArguments(args);
	    	return fragment;
	    }*/


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
	//	View view =inflater.inflate(R.layout.fragment_offre, container, false);
		
		
	//	listLists = (ListView)  findListViewById(R.id.listLists);
	//	getAllEvenement();
		
	//	Button btn = (Button) view.findViewById(R.id.addOffre);

		
		
		
		

		  View view =inflater.inflate(R.layout.fragment_offre, container, false);

		//  super.onViewCreated(container,savedInstanceState);
		  
        ViewGroup viewGroup = (ViewGroup) view;
        // As we're using a ListFragment we create a PullToRefreshLayout manually
        mPullToRefreshLayout = new PullToRefreshLayout(viewGroup.getContext());

        // We can now setup the PullToRefreshLayout
        ActionBarPullToRefresh.from(getActivity())
                // We need to insert the PullToRefreshLayout into the Fragment's ViewGroup
                .insertLayoutInto(viewGroup)
                // Here we mark just the ListView and it's Empty View as pullable
                .theseChildrenArePullable(android.R.id.list, android.R.id.empty)
                .listener(this)
                .setup(mPullToRefreshLayout);
		
		return view;
		
		
	}

	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		if(offreArray==null){
			getAllOffre();
			
			offreArray=new ListOffreAdapter(getActivity(), MyUser.listsOffre);
		//	offreArray= new ArrayAdapter<String>(getActivity(),	android.R.layout.simple_list_item_1, listsName);
			
			setListAdapter(offreArray);
			
			
           // setListShownNoAnimation(true);
			
		

		/**
			
			  // Set a listener to be invoked when the list should be refreshed.
	        ((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() {
	            @Override
	            public void onRefresh() {
	                // Do work to refresh the list here.
	                new GetDataTask().execute();
	            }
	        });*/

	     //   listsOffre = new LinkedList<Offre>();
	    //    mListItems.addAll(Arrays.asList(mStrings));

	     //   ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,           android.R.layout.simple_list_item_1, mListItems);

	     //   setListAdapter(adapter);
		}
	}

  
	/*

    @SuppressLint("NewApi")
	private class GetDataTask extends AsyncTask<Void, Void, String[]> {

        @Override
        protected String[] doInBackground(Void... params) {
            // Simulates a background job.
        	String[] strings = null;
            try {
                Thread.sleep(2000);

           //    strings= getStringsOffre();
               getAllOffre();


    			
            } catch (InterruptedException e) {

     			 Log.e("doInBackground",e.getMessage());
            }
            
            return strings;
        }

        
        @Override
        protected void onPostExecute(String[] result) {

			 Log.i("onPostExecute",String.valueOf(listsName.size()));
        	listsName.add("Added after refresh...");

            // Call onRefreshComplete when the list has been refreshed.
            ((PullToRefreshListView) getListView()).onRefreshComplete();

            super.onPostExecute(result);
        }
    }
*/
    
    
	private void getAllOffre() {

		OffreController list = new OffreController();

		//if(listsName==null)
		//	listsName  = new ArrayList<String>();
		try {
			MyUser.listsOffre.addAll(list.getAllOffre());
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (MyUser.listsOffre != null) {
		
		
		/*	for (Object o : listsOffre) {
				Offre u=(Offre) o;
				if (u != null)
				listsName.add(u.getTitre() + "\n" + u.getTexte());
			}
			Log.i("GetAllActiviteActivity",String.valueOf(listsName.size()));*/
			
		//	setListAdapter(offreArray);
			
			//listLists.setTextFilterEnabled(true);

		//	offreArray=new ListOffreAdapter(getActivity(), listsOffre);

		}
	}
	
	
	
	final void newOffre(){

		 startActivity(new Intent(getActivity(), OffreAddActivity.class));
	
    }


	@Override
	public void onRefreshStarted(View view) {
		// TODO Auto-generated method stub
		 new AsyncTask<Void, Void, Void>() {

             @Override
             protected Void doInBackground(Void... params) {
              //   try {
                     
                	 //Thread.sleep(5000); // 5 seconds 

                     getAllOffre();
                //     int itemNo=list.size();
              //       itemNo++;
               //   list.add("New Item No :"+itemNo);
	                	
                    
                     
            //     } catch (InterruptedException e) {
             //       e.printStackTrace();
             //    }
                 return null;
             }

             @Override
             protected void onPostExecute(Void result) {
                 super.onPostExecute(result);
           
                 offreArray.notifyDataSetChanged();
                 // Notify PullToRefreshLayout that the refresh has finished
                 mPullToRefreshLayout.setRefreshComplete();

         // if you set the "setListShown(false)" then you have to 
         //uncomment the below code segment
                 
//                 if (getView() != null) {
//                     // Show the list again
//                     setListShown(true);
//                 }
             }
         }.execute();
		
		
	}

}
